﻿using B.S.BasicData.API.Read.Application.Command.Leave;
using B.S.BasicData.API.Read.DTOS.Leave;
using B.S.BasicData.Domain.Entities.Leave;
using B.S.BasicData.Domain.Entities.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.API.Read.Application.Handler.Leave
{
    /// <summary>
    /// 获取申请列表请求命令处理类
    /// </summary>
    public class GetMyLeaveHandler:IRequestHandler<GetMyLeaveCommand,ApiPaging<LeaveDTO>>
    {
        IBaseRepository<LeaveApplicationModel> leaveRep;
        IBaseRepository<LeaveTypeModel> leavetypeRep;
        IBaseRepository<UserModel> userRep;

        public GetMyLeaveHandler(IBaseRepository<LeaveApplicationModel> leaveRep, IBaseRepository<LeaveTypeModel> leavetypeRep, IBaseRepository<UserModel> userRep)
        {
            this.leaveRep = leaveRep;
            this.leavetypeRep = leavetypeRep;
            this.userRep = userRep;
        }

        public Task<ApiPaging<LeaveDTO>> Handle(GetMyLeaveCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiPaging<LeaveDTO> res = new ApiPaging<LeaveDTO>();

                var ismanager = userRep.GetAll().Where(x => !x.IsDeleted && x.UserName == request.loginname).FirstOrDefault();

                var leaves = new List<LeaveApplicationModel>();
                var leavetypes=new List<LeaveTypeModel>();

                if (ismanager != null)
                {
                    if (ismanager.IsManager == false)
                    {
                        leaves = leaveRep.GetAll().Where(x => !x.IsDeleted && x.ApplicationName == request.loginname).ToList();
                        leavetypes = leavetypeRep.GetAll().ToList();

                        
                    }
                    else
                    {
                         leaves = leaveRep.GetAll().Where(x => !x.IsDeleted &&x.DepartmentId== ismanager.DepartmentId).ToList();
                        leavetypes = leavetypeRep.GetAll().ToList();
                    }

                }
                else
                {
                    res.Msg = "查无此人";
                    res.Code = ApiEnum.Fail;
                    return Task.FromResult(res);
                }

                if (request.statetime != null && request.endtime != null)
                {
                    leaves = leaves.Where(x => x.CreateDate >= request.statetime && x.CreateDate <= request.endtime.Value.AddDays(1)).ToList();
                }

                if (request.state != null)
                {
                    leaves = leaves.Where(x => x.State == request.state).ToList();
                }

                if (!string.IsNullOrEmpty(request.leavename))
                {
                    leaves = leaves.Where(x => x.ApplicationName.Contains(request.leavename)).ToList();
                }

                var linq = from a in leaves
                           join b in leavetypes on a.LeaveTypeId equals b.Id
                           select new LeaveDTO
                           {
                               Id = a.Id,
                               ApplicationName = a.ApplicationName,
                               DepartmentId = a.DepartmentId,
                               LeaveTypeId = a.LeaveTypeId,
                               LeaveTypeName = b.LeaveTypeName,
                               StartTime = a.StartTime,
                               EndTime = a.EndTime,
                               TotalTime = a.TotalTime,
                               ReportTime = a.ReportTime,
                               Reason = a.Reason,
                               Depleaders = a.Depleaders,
                               State = a.State,
                               CreateDate = a.CreateDate,
                               LeaveReview=a.LeaveReview
                           };
                res.TotalCount = linq.Count();
                res.TotalPage = (int)Math.Ceiling(res.TotalCount * 1.0 / request.pagesize);
                res.PageDatas = linq.OrderByDescending(x => x.Id).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();



                res.Msg = "获取申请列表";
                res.Code = ApiEnum.Success;

                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
